require 'cinch'

class BenchmarkedLogger
    class IrcBotSink
        def initialize(bot)
            @bot = bot
        end

        def <<(message)
            @bot.handlers.dispatch(:log_info,nil,message)
        end
    end
    class IrcBotSink2 < DelegateClass(Cinch::Bot)
        def <<(message)
           handlers.dispatch(:log_info,nil,message)
        end
    end
    def initialize(sink)
        @sink=case sink
        when Cinck::Bot then IrcBotSink.new(sink)
        else sink
        end
    end

    def info(message)
        start_time = Time.now
        yield
        duration = start_time - Time.now
        @sink << ("[%1.3f] %s\n" % [duration,message])
    end
end

log = BenchmarkedLogger.new('buffered.log')
log.info("mark"){1000.times do puts 'test' end}

bot = Cinch::Bot.new do
    configure do |c|
        c.nick = "bm-logger"
        c.server = ENV["LOG_SERVER"]
        c.channels = [ENV["LOG_CHANNEl"]]
        c.verbose = true
    end
    on :log_info do |m, line|
        Channel(ENV["LOG_CHANNEL"]).msg(line)
    end
end
bot_thread = Thread.new do
    bot.start
end

bot.handlers.dispatch(:log_info,nil,"Something happened...")